package controller

import (
	"github.com/cn-ygf/go-admin/internal/action"
	"github.com/cn-ygf/go-admin/internal/modular/system/model"
	"github.com/cn-ygf/go-admin/internal/modular/system/service"
	"github.com/cn-ygf/go-admin/internal/modular/system/vo"
	"github.com/cn-ygf/go-admin/pkg/utils"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
)

// DictDataController 字典管理
type DictDataController struct {
	Ctx iris.Context
}

// GetTypeBy 根据字典type获取values
// /system/dict/data/type/:name
func (c *DictDataController) GetTypeBy(dictTypeName string) mvc.Result {
	result, err := service.DictDataService.GetValues(dictTypeName)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.Data(result)
}

// PostList 分页查询
func (c *DictDataController) PostList(req vo.QueryDictDataListReq) mvc.Result {
	if !hasPermi(c.Ctx, "system:dict:list") {
		return nil
	}
	var (
		err   error
		list  []*model.SysDictDatum
		total int64
	)
	if req.PageSize > 1000 {
		req.PageSize = 1000
	}
	list, total, err = service.DictDataService.QueryList(&req)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.Data(map[string]any{
		"rows":  list,
		"total": total,
	})
}

// GetBy 根据id获取字典信息
func (c *DictDataController) GetBy(id int64) mvc.Result {
	if !hasPermi(c.Ctx, "system:dict:query") {
		return nil
	}
	dictData, err := service.DictDataService.GetByID(id)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.Data(dictData)
}

// Put 修改字典
func (c *DictDataController) Put(req vo.EditDictDataReq) mvc.Result {
	if !hasPermi(c.Ctx, "system:dict:edit") {
		return nil
	}
	var (
		err error
	)
	req.UpdateBy = getCurrentUser(c.Ctx).UserName
	err = service.DictDataService.Update(&req)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.OK()
}

// Post 添加字典
func (c *DictDataController) Post(req vo.AddDictDataReq) mvc.Result {
	if !hasPermi(c.Ctx, "system:dict:add") {
		return nil
	}
	var (
		err error
	)
	req.CreateBy = getCurrentUser(c.Ctx).UserName
	err = service.DictDataService.Add(&req)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.OK()
}

// DeleteBy 删除字典
func (c *DictDataController) DeleteBy(idStr string) mvc.Result {
	if !hasPermi(c.Ctx, "system:dict:remove") {
		return nil
	}
	var (
		err error
	)
	ids := utils.IdsStrToInt64(idStr)
	if len(ids) < 1 {
		return action.ParamError()
	}

	err = service.DictDataService.Delete(ids)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.OK()
}

// PostExport 导出字典
func (c *DictDataController) PostExport() mvc.Result {
	if !hasPermi(c.Ctx, "system:dict:export") {
		return action.RawError()
	}
	dictType := c.Ctx.Request().PostFormValue("dictType")
	if dictType == "" {
		return action.RawError()
	}
	dictValueList, err := service.DictDataService.GetValuesEx(dictType)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.RawError()
	}

	titleRow := []string{"字典编码", "字典标签", "字典键值", "字典类型", "是否默认", "状态"}
	statusMaps, err := service.DictDataService.GetDictMaps("sys_normal_disable")
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.RawError()
	}
	data := make([][]any, len(dictValueList))
	for index, item := range dictValueList {
		row := make([]any, 6)
		row[0] = item.DictCode
		row[1] = item.DictLabel
		row[2] = item.DictValue
		row[3] = item.DictType
		row[4] = item.IsDefault
		row[5] = statusMaps[item.Status]
		data[index] = row

	}
	return action.ExportExcel(data, titleRow)
}
